Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use Bun's implementation of ws instead of the bundled one #13901

Merged
merged 1 commit into from
Jul 21, 2023
Merged

fix: use Bun's implementation of ws instead of the bundled one #13901

merged 1 commit into from
Jul 21, 2023

Conversation

paperdave
Copy link
Contributor

Description

There are two small issues that are blocking Vite from working in Bun, which is much easier to fix in the Vite project itself.

  • Bun implements it's own version of ws, which gets overridden when doing import {...} from "ws". Vite bundles it's own copy of ws which attempts to use req.socket.write which we do not implement as we do not use a node stream for the underlying network sockets. Implementing the APIs needed for the JS implementation of ws would take too long and be much slower.

  • During config loading there is a directory caching bug, the workaround we are using is applying a query string parameter to the file:// url used when loading the config through esm.

That is all that it takes to get Vite working on Bun, though it depends on a handful of various fixes we recently made to our node compatibility. To test these changes, you need to use the canary version bun upgrade --canary (or 0.7 when released), and then use bun --bun run dev (--bun forces bun's runtime over node's).

Closes oven-sh/bun#250

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Jarred-Sumner Jarred-Sumner mentioned this pull request Jul 20, 2023
@bluwy bluwy changed the title Make vite work in Bun feat: make vite work in Bun Jul 20, 2023
Comment on lines 1183 to 1186
// Bun will cache file/directories, and since we just created the file it might throw a ResolveError
// Adding a query string with the `?` will bust the cache.
// See https://github.com/oven-sh/bun/issues/3216
const fileUrl = `${pathToFileURL(fileBase)}.mjs?`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not oppose to this change, but I feel like Bun should ultimately fix this too. Currently the issue is closed and appending ? is the way in Bun?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we could move forward with this one but it will be good to remove this hack as soon as Bun works out this bug. I think we should only append ? when process.versions.bun.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bun's builtin TypeScript, JSX, ESM, and CommonJS support means transpiling a configuration file ahead of time should be unnecessary. Reading, transpiling, saving and then reading the configuration file again makes Vite start slower than it could. Rather than conditionally adding a ?, what if we plan to disable this step entirely in Bun?

Copy link
Member

@bluwy bluwy Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would lose slight parity with the current config loader, as it tracks the dependencies of the config to trigger a server restart. Personally, I think this "feature" has blocked us from simplifying a lot of things that I'd rather not have it (around Vite 5? Not sure yet).

So I guess we have two options (that I'm fine either ways):

  1. Use ? cache bust, but Bun should fix this eventually.
  2. Load config with Bun without deps restart trigger. Bun users have slightly different experience.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean toward using ? here if we can't get the same experience.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jarred-Sumner replying to your comment (#13901 (comment)), I think it would be best if we can implement the least changes on our end here to support Bun. If no2 requires using more Bun-specific APIs, that would likely create another code path to maintain.

Is oven-sh/bun#3216 (the ? issue) not fixable on Bun's end? I feel like this quirk could affect more libraries in general than only Vite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we put the ? is because the fix for bun isn't trivial. I agree we should fix this in bun, but it might not happen today for 0.7.

@patak-dev
Copy link
Member

@paperdave thanks for the PR. Would you separate the directory caching bug part on a separate PR so we can better track both changes? (and revert this one later if we go forward with it). We would like to know for this part what are your plans, as it seems we are patching a bug that may be solved soon in Bun.

This PR could remain about Bun's own version of ws, which I think we can merge without much discussion.

@Jarred-Sumner
Copy link

Jarred-Sumner commented Jul 20, 2023 via email

@paperdave
Copy link
Contributor Author

thanks for the PR. Would you separate the directory caching bug part on a separate PR so we can better track both changes? (and revert this one later if we go forward with it). We would like to know for this part what are your plans, as it seems we are patching a bug that may be solved soon in Bun.

This PR could remain about Bun's own version of ws, which I think we can merge without much discussion.

I think that is reasonable. I'll split this into two PRs.

@paperdave paperdave changed the title feat: make vite work in Bun fix: use Bun's implementation of ws instead of the bundled one Jul 20, 2023
@paperdave
Copy link
Contributor Author

the ? thing has been fixed in bun with a less-than-ideal solution but it will work for this. this should also mean older versions of vite will get past config loading, but only break on hmr.

@patak-dev patak-dev merged commit 049404c into vitejs:main Jul 21, 2023
13 checks passed
@patak-dev
Copy link
Member

Thanks for moving the ? workaround to Bun's side @paperdave. We released this PR as part of vite@4.4.6

@paperdave
Copy link
Contributor Author

legendary. were gonna release bun 0.7 tomorrow (i think) so that will be when users can test it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vite
5 participants